<?php

namespace Admin\Controller;

class NavController extends CommonController
{
    /**
     * 栏目列表
     */
    public function lst()
    {
        $nav = D('nav');
        $list = $nav->where('state != 2')->order('sort desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 栏目添加
     */
    public function add()
    {
        $nav = D('nav');
        if (IS_POST) {
            $data['title'] = I('title');
            if ($nav->create($data)) {
                if ($nav->add()) {
                    $this->success('添加栏目成功！', U('lst'));
                } else {
                    $this->error('添加栏目失败！');
                }
            } else {
                $this->error($nav->getError());
            }
            return;
        }
        $this->display();
    }
    /**
     * 栏目修改
     */
    public function edit()
    {
        $nav = D('nav');
        if (IS_POST) {
            if ($nav->create()) {
                if ($nav->save()) {
                    $this->success('修改栏目成功！', U('lst'));
                } else {
                    $this->error('修改栏目失败！');
                }
            }else{
                $this->error($nav->getError());
            }
            return;
        }
        $navs = $nav->find(I('id'));
        $this->assign('navs', $navs);
        $this->display();
    }
    /**
     * 栏目删除
     */
    public function del()
    {
        $nav = D('nav');
        $data = $nav->find(I('id'));
        $data['state'] = 2;
        if ($nav->save($data)) {
            $this->success('删除栏目成功！', U('lst'));
        } else {
            $this->error('删除栏目失败！');
        }
    }

    /**
     * 栏目禁用
     */
    public function disable()
    {
        $nav = D('nav');
        $data = $nav->find(I('id'));
        $data['state'] = 3;
        if ($nav->save($data)) {
            $this->success('禁用栏目成功！', I('lst'));
        } else {
            $this->error('禁用栏目失败！');
        }
    }

    /**
     * 启用栏目
     */
    public function enable()
    {
        $nav = D('nav');
        $data = $nav->find(I('id'));
        $data['state'] = 1;
        if ($nav->save($data)) {
            $this->success('启用栏目成功！', U('lst'));
        } else {
            $this->error('启用栏目失败！');
        }
    }
}